package dao;

import bean.AdminRole;
import bean.PageBean;
import util.JdbcUtils;
import util.StringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 管理员权限用户的Dao
 *
 * @author 邓晴予
 */
public class AdminRoleDao {
    /**
     * 管理员权限一览
     *
     * @param pageBean  the page bean
     * @param adminRole the admin role
     * @return list
     * @throws Exception the exception
     */
    public List<Map<String, String>> adminRoleList(PageBean pageBean, AdminRole adminRole) throws Exception {
        StringBuilder sb = new StringBuilder("select * from adminRole u ");
        List<Object> para = new ArrayList<>();
        if (StringUtil.isNotEmpty(adminRole.getAdminRoleId())) {
            sb.append(" and u.adminRoleId =?");
            para.add(adminRole.getAdminRoleId());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireUser())) {
            sb.append(" and u.requireUser = ?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateUser())) {
            sb.append(" and u.updateUser =?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireActivity())) {
            sb.append(" and u.requireActivity = ?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateActivity())) {
            sb.append(" and u.updateActivity =?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireAdmin())) {
            sb.append(" and u.requireAdmin = ?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateAdmin())) {
            sb.append(" and u.updateAdmin =?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireAdvice())) {
            sb.append(" and u.requireAdvice = ?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateAdvice())) {
            sb.append(" and u.updateAdvice =?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireBicycle())) {
            sb.append(" and u.requireBicycle = ?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateBicycle())) {
            sb.append(" and u.updateBicycle =?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireCyclingrecord())) {
            sb.append(" and u.requireCyclingrecord = ?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateCyclingrecord())) {
            sb.append(" and u.updateCyclingrecord =?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireDealrecord())) {
            sb.append(" and u.requireDealrecord = ?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateDealrecord())) {
            sb.append(" and u.updateDealrecord =?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireConfig())) {
            sb.append(" and u.requireConfig = ?");
            para.add(adminRole.getRequireConfig());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateConfig())) {
            sb.append(" and u.updateConfig =?");
            para.add(adminRole.getUpdateConfig());
        }

        String sql = sb.toString().replaceFirst("and", "where");
        return JdbcUtils.queryForMap(sql, para);
    }

    /**
     * 管理员权限删除
     *
     * @param delIds the del ids
     * @return int
     * @throws Exception the exception
     */
    public int adminRoleDelete(String delIds) throws Exception {
        String sql = "delete from adminRole where adminRoleId in(?)";
        List<Object> para = new ArrayList<>();
        para.add(delIds);
        return JdbcUtils.update(sql, para);
    }

    /**
     * 管理员权限添加
     *
     * @param adminRole the admin role
     * @return boolean
     * @throws Exception the exception
     */
    public boolean adminRoleAdd(AdminRole adminRole) throws Exception {
        StringBuilder sb = new StringBuilder("insert into adminRole values(");
        List<Object> para = new ArrayList<>();
        if (StringUtil.isNotEmpty(adminRole.getAdminRoleId())) {
            sb.append(" ,?");
            para.add(adminRole.getAdminRoleId());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireUser())) {
            sb.append(" ,?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateUser())) {
            sb.append(" ,?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireActivity())) {
            sb.append(" ,?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateActivity())) {
            sb.append(" ,?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireAdmin())) {
            sb.append(" ,?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateAdmin())) {
            sb.append(" ,?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireAdvice())) {
            sb.append(" ,?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateAdvice())) {
            sb.append(" ,?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireBicycle())) {
            sb.append(" ,?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateBicycle())) {
            sb.append(" ,?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireCyclingrecord())) {
            sb.append(" ,?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateCyclingrecord())) {
            sb.append(" ,?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireDealrecord())) {
            sb.append(" ,?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateDealrecord())) {
            sb.append(" ,?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireConfig())) {
            sb.append(" ,?");
            para.add(adminRole.getRequireConfig());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateConfig())) {
            sb.append(" ,?");
            para.add(adminRole.getUpdateConfig());
        }
        sb.append(")");
        String sql = sb.toString().replaceFirst(",", " ");
        return 1 == JdbcUtils.update(sql, para);
    }

    /**
     * 管理员权限用户修改
     *
     * @param adminRole the admin role
     * @return int
     * @throws Exception the exception
     */
    public int adminRoleModify(AdminRole adminRole) throws Exception {
        StringBuilder sb = new StringBuilder("update adminRole u ");
        List<Object> para = new ArrayList<>();
        if (StringUtil.isNotEmpty(adminRole.getRequireUser())) {
            sb.append(" , u.requireUser = ?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateUser())) {
            sb.append(" , u.updateUser =?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireActivity())) {
            sb.append(" , u.requireActivity = ?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateActivity())) {
            sb.append(" , u.updateActivity =?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireAdmin())) {
            sb.append(" , u.requireAdmin = ?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateAdmin())) {
            sb.append(" , u.updateAdmin =?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireAdvice())) {
            sb.append(" , u.requireAdvice = ?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateAdvice())) {
            sb.append(" , u.updateAdvice =?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireBicycle())) {
            sb.append(" , u.requireBicycle = ?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateBicycle())) {
            sb.append(" , u.updateBicycle =?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireCyclingrecord())) {
            sb.append(" , u.requireCyclingrecord = ?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateCyclingrecord())) {
            sb.append(" , u.updateCyclingrecord =?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireDealrecord())) {
            sb.append(" , u.requireDealrecord = ?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateDealrecord())) {
            sb.append(" , u.updateDealrecord =?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireConfig())) {
            sb.append(" , u.requireConfig = ?");
            para.add(adminRole.getRequireConfig());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateConfig())) {
            sb.append(" , u.updateConfig =?");
            para.add(adminRole.getUpdateConfig());
        }
        sb.append(" where u.adminRoleId =?");
        para.add(adminRole.getAdminRoleId());
        String sql = sb.toString().replaceFirst(",", "set");
        return JdbcUtils.update(sql, para);
    }

    /**
     * Admin role count int.
     *
     * @param adminRole the admin role
     * @return the int
     * @throws Exception the exception
     */
    public int adminRoleCount(AdminRole adminRole) throws Exception {
        StringBuilder sb = new StringBuilder("select count(*) as total from adminRole u");

        List<Object> para = new ArrayList<Object>();
        if (StringUtil.isNotEmpty(adminRole.getAdminRoleId())) {
            sb.append(" and u.adminRoleId =?");
            para.add(adminRole.getAdminRoleId());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireUser())) {
            sb.append(" and u.requireUser = ?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateUser())) {
            sb.append(" and u.updateUser =?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireActivity())) {
            sb.append(" and u.requireActivity = ?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateActivity())) {
            sb.append(" and u.updateActivity =?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireAdmin())) {
            sb.append(" and u.requireAdmin = ?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateAdmin())) {
            sb.append(" and u.updateAdmin =?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireAdvice())) {
            sb.append(" and u.requireAdvice = ?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateAdvice())) {
            sb.append(" and u.updateAdvice =?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireBicycle())) {
            sb.append(" and u.requireBicycle = ?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateBicycle())) {
            sb.append(" and u.updateBicycle =?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireCyclingrecord())) {
            sb.append(" and u.requireCyclingrecord = ?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateCyclingrecord())) {
            sb.append(" and u.updateCyclingrecord =?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireDealrecord())) {
            sb.append(" and u.requireDealrecord = ?");
            para.add(adminRole.getRequireUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateDealrecord())) {
            sb.append(" and u.updateDealrecord =?");
            para.add(adminRole.getUpdateUser());
        }
        if (StringUtil.isNotEmpty(adminRole.getRequireConfig())) {
            sb.append(" and u.requireConfig = ?");
            para.add(adminRole.getRequireConfig());
        }
        if (StringUtil.isNotEmpty(adminRole.getUpdateConfig())) {
            sb.append(" and u.updateConfig =?");
            para.add(adminRole.getUpdateConfig());
        }

        String sql = sb.toString().replaceFirst("and", "where");
        List<Map<String, String>> result = JdbcUtils.queryForMap(sql, para);
        return result.isEmpty() ? 0 : Integer.parseInt(result.get(0).get("total"));
    }
}
